package com.pay.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSON;
import com.pay.beans.ReturnBean;
import com.pay.beans.ReturnContent;
import com.pay.common.util.PropertyUtil;
import com.pay.constants.RedisKeyConstants;
import com.pay.constants.RetConstants;
import com.pay.constants.WeixinConstants;
import com.pay.dubbo.wx.WxCustomerServiceFacade;
import com.pay.util.NeedOpenId;
import com.pay.util.RedisUtil;
import com.pay.util.RequestUtil;

@Controller
@RequestMapping("customer")
@SuppressWarnings("all")
public class WxCustomerController {

	private Log log = LogFactory.getLog(WxCustomerController.class);
	private static String staticResHomeUrl;
	static {
		PropertyUtil propertyUtil = PropertyUtil.getInstance("wxweb");
		staticResHomeUrl = propertyUtil.getProperty("com.pay.wx.staticResHomeUrl");
	}
	@Resource
	private WxCustomerServiceFacade wxCustomerServiceFacade;

	@RequestMapping(value = "logincheck", produces = "text/html;charset=UTF-8")
	@NeedOpenId(needOpenId = true, isLoginFlag = false)
	public @ResponseBody String logincheck(HttpServletRequest httpRequest,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = httpRequest.getParameter("openId");
		String username = httpRequest.getParameter("username");
		String password = httpRequest.getParameter("password");
		log.info("login logincheck log openId:" + openId + ",username:" + username );
		String returnStr = null;
		try {
			ReturnBean returnBean = wxCustomerServiceFacade.logincheck(username, password, openId);
			log.info("login logincheck result:" + returnBean.toString());
			returnStr = JSON.toJSONString(returnBean);
			if (RetConstants.SUCCESS.equals(returnBean.getCode())) {
				returnStr = JSON.toJSONString(returnBean);
			}
		} catch (Exception e) {
			log.error("login logincheck error openId:" + openId);
			log.info("", e);
			return JSON.toJSONString(new ReturnBean(RetConstants.SYSTEM_ERROR, RetConstants.EXCEPTION_ERROR_MSG));
		}

		return returnStr;
	}

	@RequestMapping(value = "loginPage")
	@NeedOpenId(needOpenId = true, isLoginFlag = false)
	public String loginJuge(HttpServletRequest httpRequest,HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = (String) WebUtils.getSessionAttribute(httpRequest, WeixinConstants.sessionOpenIdKey);
		String customerNo = RedisUtil.getValue(RedisKeyConstants.MULT_ACCOUNT_DEFAULT_OPENID_USERNO + openId);
		String requestURL = httpRequest.getRequestURL().toString();
		log.info("查询到的customerNo：" + customerNo);
		
		if (StringUtils.isBlank(customerNo)) {
			log.info("多账户中未查到登录帐户");
			log.info("openId:{"+openId+"}尚未进行过绑定，请重新登录");
			String requestParammStr = RequestUtil.getRequestAuthParam(httpRequest);
			StringBuffer sBuffer = new StringBuffer(staticResHomeUrl);
			sBuffer.append("/loginPage/loginPage.html");
			if (!StringUtils.isEmpty(requestParammStr)) {
				sBuffer.append("?");
				sBuffer.append(requestParammStr);
				sBuffer.append("&");
			} else {
				sBuffer.append("?");
			}
			sBuffer.append("openId=");
			sBuffer.append(openId);
			return "redirect:" + sBuffer.toString();
		}else{
			//成功
			String requestParammStr = RequestUtil.getRequestAuthParam(httpRequest);
			StringBuffer sBuffer = new StringBuffer(staticResHomeUrl);
			sBuffer.append("/loginPage/loggedInPage.html");
			if (!StringUtils.isEmpty(requestParammStr)) {
				sBuffer.append("?");
				sBuffer.append(requestParammStr);
				sBuffer.append("&");
			} else {
				sBuffer.append("?");
			}
			sBuffer.append("openId=");
			sBuffer.append(openId);
			sBuffer.append("&customerNo=");
			sBuffer.append(customerNo);
			return "redirect:" + sBuffer.toString();
		}
	}
	
	
	
	@RequestMapping(value = "logout", produces = "text/html;charset=UTF-8")
	@NeedOpenId(needOpenId = true, isLoginFlag = true)
	public @ResponseBody String logout(HttpServletRequest httpRequest,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = httpRequest.getParameter("openId");
		String customerNo = RedisUtil.getValue(RedisKeyConstants.MULT_ACCOUNT_DEFAULT_OPENID_USERNO + openId);
		log.info("logOut  log openId:" + openId + ",redisCustomerNo:"+customerNo);
		if (StringUtils.isBlank(customerNo)) {
			return JSON.toJSONString(new ReturnBean(RetConstants.NO_LOGIN_ERROR, RetConstants.NO_LOGIN_ERROR_MSG));
		}
		String returnStr = null;
		try {
			ReturnContent<Object> returnBean = wxCustomerServiceFacade.unbindingCustRelation(customerNo,"myself");
			log.info("logOut  result:" + returnBean.toString());
			returnStr = JSON.toJSONString(returnBean);
			if (RetConstants.SUCCESS.equals(returnBean.getCode())) {
				returnStr = JSON.toJSONString(returnBean);
			}
		} catch (Exception e) {
			log.error("logOut  error openId:" + openId);
			log.info("", e);
			return JSON.toJSONString(new ReturnBean(RetConstants.SYSTEM_ERROR, RetConstants.EXCEPTION_ERROR_MSG));
		}
		return returnStr;
	}



}
